package org.pictual.service;

import org.apache.log4j.Logger;
import org.pictual.business.dao.DAOFactory;
import org.pictual.business.dao.MySQLDAOFactory;
import org.pictual.business.dao.SequenceGeneratorDAO;
import org.pictual.business.dao.exceptions.DAOException;
import org.pictual.business.vo.ImagenVO;


public class ImagenDelegateImpl implements ImagenDelegate{

	private static ImagenDelegateImpl _instance = null;
	protected static Logger log= Logger.getLogger(ImagenDelegateImpl.class);
	protected SequenceGeneratorDAO sequenceGeneratorDAO;
	
	private ImagenDelegateImpl(){}

	public static ImagenDelegateImpl getInstance(){
		if(_instance==null){
    		_instance = new ImagenDelegateImpl();
    	}
        return _instance;
	}

	public ImagenVO readImagen(int imagen_id) {
		try {
			ImagenVO imagenVO = DAOFactory.getDAOFactory(DAOFactory.MYSQL).getImagenDAO().readImagen(imagen_id);
//			MySQLDAOFactory.closeConnection();
			return imagenVO;
		} catch (DAOException e) {
			log.error("readImagen: DAOException: ",e);
		}
		return null;
	}

	public void insertImagen(ImagenVO imagenVO) {
		try {
			int id=new Integer(SequenceGeneratorDelegateImpl.getInstance().generateId("imagen"));
			log.info("El id de la imagen es: "+id);
			imagenVO.setImagen_id(id);
			DAOFactory.getDAOFactory(DAOFactory.MYSQL).getImagenDAO().insertImagen(imagenVO);
//			MySQLDAOFactory.closeConnection();
			
		} catch (DAOException e) {
			log.error("insertImagen: DAOException: ",e);
		}
		
	}

	public void setSequenceGeneratorDAO(SequenceGeneratorDAO sequenceGeneratorDAO) {
		this.sequenceGeneratorDAO = sequenceGeneratorDAO;
	}
		
}
